define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'system.addons/index',
        add_url: 'system.addons/add',
        edit_url: 'system.addons/edit',
        delete_url: 'system.addons/delete',
        export_url: 'system.addons/export',
        modify_url: 'system.addons/modify',
        manage_url: 'system.addons/manage',
        manage_field_url: 'system.addons/manage_field',
        install_url: 'system.addons/install',
        uninstall_url: 'system.addons/uninstall',
        enable_url: 'system.addons/enable',
        disable_url: 'system.addons/disable',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh'],
                defaultToolbar: false,
                page: false,
                cols: [[
                    {field: 'title', title: '插件标题'},
                    {field: 'author', title: '作者'},
                    {field: 'version', title: '版本号'},
                    {field: 'identifier', title: '插件唯一标识符'},
                    {
                        field: 'admin',
                        search: 'select',
                        selectList: ea.getDataBrage('select_list_admin'),
                        title: '是否有后台管理'
                    },
                    {
                        field: 'status',
                        search: 'select',
                        selectList: ea.getDataBrage('select_list_status'),
                        title: '状态'
                    },
                    // {field: 'create_time', title: '安装时间'},
                    {
                        width: 250, title: '操作', templet: ea.table.tool, fixed: 'right', operat: [
                            [{
                                text: '管理',
                                url: init.manage_field_url,
                                method: 'open',
                                class: 'layui-btn layui-btn-normal layui-btn-xs',
                                _if: function (data, operat) {
                                    return data.status === 1 && data.admin === 1;
                                }
                            }],
                            [{
                                text: '配置',
                                url: init.manage_url,
                                method: 'open',
                                class: 'layui-btn layui-btn-normal layui-btn-xs',
                                _if: function (data, operat) {
                                    return data.status === 1;
                                },
                                field: 'name',
                            }],
                            [{
                                text: '安装',
                                url: init.install_url,
                                method: 'request',
                                class: 'layui-btn layui-btn-primary layui-btn-xs',
                                _if: function (data, operat) {
                                    return data.status === -1;
                                },
                                field: 'name',
                            }],
                            [{
                                text: '卸载',
                                url: init.uninstall_url,
                                method: 'request',
                                class: 'layui-btn layui-btn-danger layui-btn-xs',
                                _if: function (data, operat) {
                                    return data.status === 0;
                                },
                                field: 'name',
                            }],
                            [{
                                text: '启用',
                                url: init.enable_url,
                                method: 'request',
                                class: 'layui-btn layui-btn-success layui-btn-xs',
                                _if: function (data, operat) {
                                    return data.status === 0;
                                },
                                field: 'name',
                            }],
                            [{
                                text: '禁用',
                                url: init.disable_url,
                                method: 'request',
                                class: 'layui-btn layui-btn-warm layui-btn-xs',
                                _if: function (data, operat) {
                                    return data.status === 1;
                                },
                                field: 'name',
                            }],
                        ]
                    },

                ]],
            })
            ;

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        manage: function () {
            ea.listen();
        },
        manage_field: function () {
            var columns = ea.getDataBrage('columns');
            var id = ea.getDataBrage('id');
            var init = {
                table_elem: '#currentTable',
                table_render_id: 'currentTableRenderId',
                index_url: 'system.addons/manage_field?id=' + id,
                add_url: 'system.addons/manage_field_add?id=' + id,
                edit_url: 'system.addons/manage_field_edit',
                delete_url: 'system.addons/manage_field_delete',
            };
            columns.push({
                width: 250, title: '操作', templet: ea.table.tool, fixed: 'right', operat: [
                    "edit",
                    'delete'
                ]
            })
            ea.table.render({
                init: init,
                cols: [
                    columns,
                ],
            });

            ea.listen();
        },
        manage_field_add: function () {
            ea.listen();
        },
        manage_field_edit: function () {
            ea.listen();
        },
    };
    return Controller;
});